
/*
 * Copyright (c) 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */

#include "PvImageType.h"

#include <tdds/xtypes/DynamicTypeFactory.hpp>
#include <tdds/xtypes/DynamicType.hpp>
#include <tdds/xtypes/DynamicTypeMember.hpp>

#include "dds/type/dynamic/detail/DynamicTypeFactory.h"
#include "dds/type/dynamic/detail/DynamicType.h"
#include "dds/type/dynamic/detail/DynamicTypeMember.h"

using dds::type::dynamic::DynamicTypeFactory;
using dds::type::dynamic::MemberDescriptor;
using dds::type::dynamic::DynamicTypeMember;
using dds::type::MemberId;
using dds::type::dynamic::DynamicTypeMembersByName;
using dds::type::dynamic::DynamicTypeMembersById;

namespace epics { namespace rec { namespace oxtypes {

PvImageType::PvImageType() {

  ::oxtypes::delegate::DynamicType* dt = 
    new ::oxtypes::delegate::DynamicType();

  int nmembers = 5;

  std::vector<std::string> memberNames (nmembers);
  memberNames[0] = "id";
  memberNames[1] = "width";
  memberNames[2] = "height";
  memberNames[3] = "bits";
  memberNames[4] = "time";

  std::vector<dds::type::TypeKind> memberKinds (nmembers);
  memberKinds[0] = dds::type::INT_32_TYPE;
  memberKinds[1] = dds::type::INT_32_TYPE;
  memberKinds[2] = dds::type::INT_32_TYPE;
  memberKinds[3] = dds::type::INT_32_TYPE;
  memberKinds[4] = dds::type::INT_64_TYPE;

  // Type descriptor

  dt->td.name("org.epics.rec.oxtypes.PvImageType");
  dt->td.kind(dds::type::STRUCTURE_TYPE);

  // Member descriptors

  DynamicTypeFactory dtf = DynamicTypeFactory::get_instance();

  dt->members.reset(new DynamicTypeMembersByName);
  dt->memberByIds.reset(new DynamicTypeMembersById);

  for(unsigned int i=0; i < nmembers; i++){

     MemberDescriptor md;
     md.name(memberNames[i]);
     md.id(i);
     md.index(i);
     md.type(dtf.primitive_type(memberKinds[i]));

     DynamicTypeMember dtm(
	   new dds::type::dynamic::detail::DynamicTypeMember());
     dtm.descriptor(md);

     dt->members->insert(
       std::pair<std::string, DynamicTypeMember> (dtm.name(), dtm));
     dt->memberByIds->insert(
       std::pair<MemberId, DynamicTypeMember> (dtm.id(), dtm)); 

   }

   // delegate

   d_ = dds::type::dynamic::detail::DynamicType(dt); 

}

}}}
